import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np


# 创建 Tkinter 应用程序
root = tk.Tk()
root.title("Matplotlib in Tkinter")

# 创建 Matplotlib 图形
fig = Figure(figsize=(5, 4), dpi=100)
plot1 = fig.add_subplot(211)  # 创建第一个子图
plot2 = fig.add_subplot(212)  # 创建第二个子图

# 在子图中绘制一些内容
x = [1, 2, 3, 4, 5]
y1 = [10, 5, 8, 7, 2]
y2 = [8, 6, 2, 5, 3]
plot1.plot(x, y1, label='Line 1')
plot2.plot(x, y2, label='Line 2')

# 设置子图标题和图例
plot1.set_title('Subplot 1')
plot2.set_title('Subplot 2')
plot1.legend()
plot2.legend()

# 创建 Matplotlib 的画布并嵌入到 Tkinter 窗口中
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

# 运行 Tkinter 应用程序
root.mainloop()
